export default {
	data() {
		return {
			theme: 2,
			isShowQrCode: false, // 是否显示公众号二维码
			role: 0, //当前设定的角色： 1为消费者，2为上货员
			isManage: false, // 当前用户是否为上货员
			contactPhone: "--", // 联系电话
			isshowSub: true,
			user: '',
			CouponList: [],
			content: "获得优惠券",
			title: "恭喜你",
			couponDiscounts: '',
			// 优惠券
			couponData: {
				useList: [],
				stopList: []
			},
			CouponIdList: "",
			CouponStr: '',
			CouponNum: 0,
			domain: "",
			unlogin: false,
			kefuPhone: '',
			avatarUrl:'',
		}
	},
	/*监听页面显示*/
	onShow() {
		let token = wx.getStorageSync("token")
		this.avatarUrl=this.$store.state.avatarUrl
		this.domain = this.$api.Domain
		if (token == '' || token == null || token == undefined) {
			//不存在则跳转至登录页
			this.unlogin = true
		} else {
			this.unlogin = false

			this.getVipScore()
			this.user = JSON.parse(wx.getStorageSync("user"))
			console.log(this.user)
			this.user.avatarUrl = this.user.sex == 1 ? '../../static/Index/Man.jpg' : '../../static/Index/Women.jpg'
			this.user.passphone = this.user.phone.substring(0, 3) + '*****' + this.user.phone.slice(7)
			if (this.user.isVip == 1)
				this.CouponUserList(1)
		}

	},
	methods: {
		getToken() {
			let token = wx.getStorageSync("token")

			// this.$refs.popup.open('center')
			if (token == '' || token == null || token == undefined) {
				//不存在则跳转至登录页
				uni.navigateTo({
					url: "/pages/login/login?type=index"
				})
				return;
			}
		},
		ToYouHuiJuan() {
			this.getToken()

			if (this.user.isVip == 0) {
				this.BeCameVIP()
				return
			}
			uni.navigateTo({
				url: "/pages/offerCoupons/offerCoupons"
			})

		},
		handleGetClick(event) {
			this.LinQuCoupon()
			this.$refs.coupon_pop.show = false
		},
		LinQuCoupon() {
			this.getToken()
			let url = this.$api.LinQuCoupon + "?idList=" + this.CouponIdList
			this.$request(url, null, "GET",
					false).then(res => {
					if (res.code == 200) {

						this.CouponUserList(1)
					} else {
						uni.showToast({
							title: res.description,
							icon: 'error'
						});
					}
				})
				.catch(res => {
					uni.hideLoading();
				})

		},
		ToVipCode() {
			this.getToken()
			if (this.user.isVip == 0) {
				this.BeCameVIP()
				return
			}
			uni.navigateTo({
				url: "/pages/ToVipCode/ToVipCode"
			})
		},
		ToPoints() {
			if (this.user.isVip == 0) {
				this.BeCameVIP()
				return
			}
			// uni.navigateTo({
			// 	url: "/pages/offerCoupons/index"
			// })

		},
		getVipScore() {
			this.$request(this.$api.getVipScore, null, "GET",
					false).then(res => {
					if (res.code == 200) {
						wx.setStorageSync("user", JSON.stringify(res.data))
						this.user = res.data
					} else {
						uni.showToast({
							title: res.description,
							icon: 'error'
						});
					}
				})
				.catch(res => {
					uni.hideLoading();
				})
		},
		CouponUserList(type) {

			let url = this.$api.CouponUserList + '?type=' + type
			this.$request(url, null, "GET",
					false).then(res => {
					if (res.code == 200) {

						this.CouponNum = res.data.length

						this.$forceUpdate()
					} else {
						uni.showToast({
							title: res.description,
							icon: 'error'
						});
					}
				})
				.catch(res => {
					uni.hideLoading();
				})
		},

		Logout() {
			this.$store.state.storeId = ''
			uni.clearStorageSync()
			uni.navigateTo({
				url: "/pages/login/login"
			})
		},
		showUser() {
			this.getToken()
			uni.showModal({
				title: "小提示",
				content: "在收银机出示会员码购买商品即可积会员分~"
			})
		},
		ToJiFen() {
			this.getToken()
			if (this.user.isVip == 0) {
				this.BeCameVIP()
				return
			}
			uni.showModal({
				title: "积分商城可以使用积分兑换商品",
				content: "暂未开放，技术小哥正在拼命加班..."
			})
		},
		ToJiaMen() {
			this.$refs.popup2.open('bottom')
		},
		ToYiJian() {

			let data = {
				storeId: uni.getStorageSync("storeId")
			}


			this.$request(this.$api.applyOpenDoor, data, "POST",
					false).then(res => {
					if (res.code == 200) {
						uni.showModal({
							content:` 联系客服:+${res.data.phone}`
						})
					} else {}
				})
				.catch(res => {
					uni.hideLoading();
				})			
			// uni.navigateTo({
			// 	url: "/pages/bert-suggest/suggest"
			// })
		},
		ToXiaoFei() {
			if (this.user.isVip == 0) {
				this.BeCameVIP()
				return
			}
			this.getToken()

			uni.navigateTo({
				url: "/pages/order/newOrder"
			})
		},
		BeCameVIP() {
			this.getToken()
			if (this.user.isVip == 0) {


				let storeId = uni.getStorageSync("storeId") // 就注册会员时候会用到
				// if (storeId == null || storeId == undefined || storeId == "") {
				// 	uni.showToast({
				// 		title: '请先扫附近门店',
				// 		icon: 'none'
				// 	});
				// 	return
				// }
				let url = this.$api.BecameVip
				uni.showLoading({
					title: '加载中'
				});
				this.$request(url, null, "GET",
						false).then(res => {
						if (res.code == 200) {
							uni.hideLoading();

							// uni.showToast({
							// 	title: "已成为会员！",
							// 	icon: 'success'
							// });
							uni.showToast({
								title: "成功成为会员，现在你能享受会员特权了！",
							});
							this.showUser()
							this.CheckToThereCoupon("AfterBecameVIP")

							this.user.isVip = 1
							uni.setStorageSync("user", JSON.stringify(this.user))
							// setTimeout(() => {
							// 	this.$refs.popup.open('center')
							// }, 2000)
						} else {
							uni.hideLoading();
							uni.showToast({
								title: res.description,
								icon: 'error'
							});
						}
					})
					.catch(res => {
						uni.hideLoading();
					})
			}
		},
		CheckToThereCoupon(status) {
			let data = {
				storeId: uni.getStorageSync("storeId"),
				status: status
			}
			this.$request(this.$api.CheckToThereCoupon, data, "POST",
					false).then(res => {
					if (res.code == 200) {
						if (res.data.length != 0) {
							let CouponIdList1 = []
							res.data.forEach(item => {

								if (item.type != "offerCoinCoupons") {
									let ruleFieldL = item.yzsActivityRuleList[0].ruleField.split(":")
									item.price = ruleFieldL[1]
									if (ruleFieldL[0] != 0)
										item.minPrice = ruleFieldL[0]
								} else {
									item.goodsName = item.yzsActivityRuleList[0].operationType
									item.num = item.yzsActivityRuleList[0].ruleField
								}
								item.name = item.activityName
								item.operationType = item.yzsActivityRuleList[0].operationType
								item.endData = item.yzsActivityRuleList[0].presentId
								CouponIdList1.push(item.activityId)
							})
							this.CouponList = res.data
							this.$refs.coupon_pop.show = true
							this.CouponIdList = CouponIdList1.join(",")

						}


					} else {
						uni.showToast({
							title: res.description,
							icon: 'error'
						});
					}
				})
				.catch(res => {
					uni.hideLoading();
				})
		},

		close() {
			this.$refs.popup.close()

		},
		// 点击我的
		mine: function() {
			uni.redirectTo({
				url: "/pages/mine/mine"
			})
		},

		// 报修
		fault: function() {
			uni.navigateTo({
				url: "/pages/index/index"
			})
		},
		// 开启订阅消息
		openMsg() {
			var that = this
			// 获取用户的当前设置，判断是否点击了“总是保持以上，不在询问”
			wx.getSetting({
				withSubscriptions: true, //是否获取用户订阅消息的订阅状态，默认false不返回
				success(res) {
					console.log(res)
					if (res.authSetting['scope.subscribeMessage']) { //用户点击了“总是保持以上，不再询问”
						uni.openSetting({ // 打开设置页
							success(res) {
								console.log(res.authSetting)
							}
						});
					} else { // 用户没有点击“总是保持以上，不再询问”则每次都会调起订阅消息
						var templateid = [
							'jFBgTV5atfDDz2wSSpxzLglRGIPL75tuXsk0iHRbqiU',
							'xVvYnFKgTiaDbWPOb2wQ95AbuhrNL2RJA9UbheOZr6s',
							'6GCyt4Tu3Bqv5D9IQkSwMMR0vWsLC9McGj6vy42aPfc'
						]
						uni.requestSubscribeMessage({
							tmplIds: templateid,
							success(res) {
								let successTempid = []
								for (let i in res) {
									if (res[i] == "accept") successTempid.push(i)
								}
								const data = {
									openId: that.$store.state.userInfo.openId,
									userId: that.$store.state.userInfo.userId,
									templateIdListStr: successTempid.toString(),
								}
								console.log(data)
								that.$request(that.$api.Subscribe, data, "post").then(res => {})
								uni.navigateTo({
									url: "/pages/mine/redPacket/redPacket"
								})
							},
							fail: (res) => {
								console.log(res)
								uni.navigateTo({
									url: "/pages/mine/redPacket/redPacket"
								})
							}
						})
					}
				}
			})
		},
		// 扫码获取绑定信息
		bindManage: function() {
			uni.scanCode({
				onlyFromCamera: false,
				success: (res) => {
					console.log(res)
					let paramList = res.result.split("?")[1].split("&");
					let deviceId = null;
					let name = null;
					let userId = this.$store.state.userInfo.userId;
					paramList.forEach((item, index) => {
						let key = item.split("=")[0];
						let value = item.split("=")[1];
						if (key == "deviceId") {
							deviceId = value;
						}
						if (key == "name") {
							name = value;
						}
					})
					this.pushBindManage(deviceId, name, userId);
				},
				fail: (res) => {
					console.log(res)
				},
				complete: (res) => {
					console.log(res)
				}
			});
		},

		// 绑定上货员
		pushBindManage: function(deviceId, name, userId) {
			if (!deviceId || deviceId == '') {
				uni.showToast({
					title: '没有设备id',
					icon: 'none'
				});
				return false;
			}
			if (!name || name == '') {
				uni.showToast({
					title: '没有名称',
					icon: 'none'
				});
				return false;
			}
			if (!userId || userId == '') {
				uni.showToast({
					title: '未登录',
					icon: 'none'
				});
				return false;
			}
			let data = {
				deviceId,
				name,
				userId,
			}
			this.$util.request(this.$api.BindManage, data, "POST").then(res => {
				if (res.returnCode == "SUCCESS") {
					this.$store.state.isManage = true;
					uni.showModal({
						title: '提示',
						content: res.returnMessage,
						showCancel: false,
					});
				} else {
					uni.showModal({
						title: '提示',
						content: res.returnMessage,
						showCancel: false,
					});
				}
			})
		},

		// 联系客服
		phoneContact: function() {
			uni.makePhoneCall({
				phoneNumber: this.contactPhone, // 电话
			})
		},

		// 显示公众号二维码
		showQrCode: function() {
			this.isShowQrCode = !this.isShowQrCode; // 取反			
		},
		//我的红包
		toMyPacket: function() {
			// #ifdef MP-WEIXIN
			this.openMsg();
			// #endif
			// #ifdef MP-ALIPAY
			uni.navigateTo({
				url: "/pages/mine/redPacket/redPacket"
			})
			// #endif

		},
		// 点击我的订单
		toOrder: function() {
			uni.navigateTo({
				// url: "/pages/mine/order/order"
				url: "/pages/mine/order/newOrder"
			})
		},

		// 点击我的优惠券
		toCoupon: function() {
			uni.navigateTo({
				url: "/pages/mine/coupon/coupon"
			})
		},

		// 点击常见问题
		toQuestion: function() {
			uni.navigateTo({
				url: "/pages/mine/question/question"
			})
		},

		// 点击意见反馈
		toOpinion: function() {
			if (this.$store.state.deviceId) {
				uni.navigateTo({
					url: "/pages/mine/opinion/opinion"
				})
			} else {
				uni.showModal({
					title: "提示",
					content: "请扫描设备上的二维码进入小程序",
					showCancel: false,
				})
			}
		},

		// 我的余额
		toBalance: function() {
			uni.navigateTo({
				url: "/pages/activity/shiYueWei_prepaid/balance"
			})
		},


		// 切换身份
		switchRole: function() {
			uni.navigateTo({
				url: "/pages/indexManage/switchRole/switchRole"
			})
		},

		// 设备报修
		toRepair: function() {
			uni.navigateTo({
				url: "/pages/fault/fault"
			})
		},
	}
}